<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户ID" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="用户类型：1-管理员用户，2-会员用户" prop="userType">
        <el-select v-model="formData.userType" placeholder="请选择用户类型：1-管理员用户，2-会员用户">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="活跃日期" prop="activityDate">
        <el-date-picker
          v-model="formData.activityDate"
          type="date"
          value-format="x"
          placeholder="选择活跃日期"
        />
      </el-form-item>
      <el-form-item label="是否活跃：0-否，1-是" prop="isActive">
        <el-radio-group v-model="formData.isActive">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否新用户：0-否，1-是" prop="isNewUser">
        <el-radio-group v-model="formData.isNewUser">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="登录次数" prop="loginCount">
        <el-input v-model="formData.loginCount" placeholder="请输入登录次数" />
      </el-form-item>
      <el-form-item label="首次登录时间" prop="firstLoginTime">
        <el-date-picker
          v-model="formData.firstLoginTime"
          type="date"
          value-format="x"
          placeholder="选择首次登录时间"
        />
      </el-form-item>
      <el-form-item label="最后登录时间" prop="lastLoginTime">
        <el-date-picker
          v-model="formData.lastLoginTime"
          type="date"
          value-format="x"
          placeholder="选择最后登录时间"
        />
      </el-form-item>
      <el-form-item label="在线时长（分钟）" prop="onlineDuration">
        <el-input v-model="formData.onlineDuration" placeholder="请输入在线时长（分钟）" />
      </el-form-item>
      <el-form-item label="页面浏览次数" prop="pageViews">
        <el-input v-model="formData.pageViews" placeholder="请输入页面浏览次数" />
      </el-form-item>
      <el-form-item label="API调用次数" prop="apiCalls">
        <el-input v-model="formData.apiCalls" placeholder="请输入API调用次数" />
      </el-form-item>
      <el-form-item label="功能使用次数" prop="featureUsageCount">
        <el-input v-model="formData.featureUsageCount" placeholder="请输入功能使用次数" />
      </el-form-item>
      <el-form-item label="设备类型：PC/Mobile/Tablet" prop="deviceType">
        <el-select v-model="formData.deviceType" placeholder="请选择设备类型：PC/Mobile/Tablet">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="浏览器类型" prop="browserType">
        <el-select v-model="formData.browserType" placeholder="请选择浏览器类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作系统类型" prop="osType">
        <el-select v-model="formData.osType" placeholder="请选择操作系统类型">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="IP地址" prop="ipAddress">
        <el-input v-model="formData.ipAddress" placeholder="请输入IP地址" />
      </el-form-item>
      <el-form-item label="地域信息" prop="region">
        <el-input v-model="formData.region" placeholder="请输入地域信息" />
      </el-form-item>
      <el-form-item label="城市信息" prop="city">
        <el-input v-model="formData.city" placeholder="请输入城市信息" />
      </el-form-item>
      <el-form-item label="连续活跃天数" prop="consecutiveDays">
        <el-input v-model="formData.consecutiveDays" placeholder="请输入连续活跃天数" />
      </el-form-item>
      <el-form-item label="总活跃天数" prop="totalActiveDays">
        <el-input v-model="formData.totalActiveDays" placeholder="请输入总活跃天数" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { UserDailyActivityApi, UserDailyActivityVO } from '@/api/system/userdailyactivity'

/** 用户每日活跃明细 表单 */
defineOptions({ name: 'UserDailyActivityForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  username: undefined,
  userType: undefined,
  activityDate: undefined,
  isActive: undefined,
  isNewUser: undefined,
  loginCount: undefined,
  firstLoginTime: undefined,
  lastLoginTime: undefined,
  onlineDuration: undefined,
  pageViews: undefined,
  apiCalls: undefined,
  featureUsageCount: undefined,
  deviceType: undefined,
  browserType: undefined,
  osType: undefined,
  ipAddress: undefined,
  region: undefined,
  city: undefined,
  consecutiveDays: undefined,
  totalActiveDays: undefined,
})
const formRules = reactive({
  userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
  userType: [{ required: true, message: '用户类型：1-管理员用户，2-会员用户不能为空', trigger: 'change' }],
  activityDate: [{ required: true, message: '活跃日期不能为空', trigger: 'blur' }],
  isActive: [{ required: true, message: '是否活跃：0-否，1-是不能为空', trigger: 'blur' }],
  isNewUser: [{ required: true, message: '是否新用户：0-否，1-是不能为空', trigger: 'blur' }],
  loginCount: [{ required: true, message: '登录次数不能为空', trigger: 'blur' }],
  onlineDuration: [{ required: true, message: '在线时长（分钟）不能为空', trigger: 'blur' }],
  pageViews: [{ required: true, message: '页面浏览次数不能为空', trigger: 'blur' }],
  apiCalls: [{ required: true, message: 'API调用次数不能为空', trigger: 'blur' }],
  featureUsageCount: [{ required: true, message: '功能使用次数不能为空', trigger: 'blur' }],
  consecutiveDays: [{ required: true, message: '连续活跃天数不能为空', trigger: 'blur' }],
  totalActiveDays: [{ required: true, message: '总活跃天数不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await UserDailyActivityApi.getUserDailyActivity(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as UserDailyActivityVO
    if (formType.value === 'create') {
      await UserDailyActivityApi.createUserDailyActivity(data)
      message.success(t('common.createSuccess'))
    } else {
      await UserDailyActivityApi.updateUserDailyActivity(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    username: undefined,
    userType: undefined,
    activityDate: undefined,
    isActive: undefined,
    isNewUser: undefined,
    loginCount: undefined,
    firstLoginTime: undefined,
    lastLoginTime: undefined,
    onlineDuration: undefined,
    pageViews: undefined,
    apiCalls: undefined,
    featureUsageCount: undefined,
    deviceType: undefined,
    browserType: undefined,
    osType: undefined,
    ipAddress: undefined,
    region: undefined,
    city: undefined,
    consecutiveDays: undefined,
    totalActiveDays: undefined,
  }
  formRef.value?.resetFields()
}
</script>